//
//  ZJRoomDetailController.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/13.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJRoomDetailController.h"
#import "MWPhotoBrowser.h"

@interface ZJRoomDetailController ()<MWPhotoBrowserDelegate>{

    NSInteger dateResult;
    NSString * tapStr;
}
@property (weak, nonatomic) IBOutlet UILabel *houseNumTitle; ///房间号
@property (weak, nonatomic) IBOutlet UILabel *houseNumInfo; /// 房型
@property (weak, nonatomic) IBOutlet UILabel *houseNumFollw; /// 关注数量
@property (weak, nonatomic) IBOutlet UIImageView *houseNumPhoto; /// 户型图路径
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UIImageView *direction; // 朝向
@property (strong, nonatomic)UIAlertController *alertController;
@end

@implementation ZJRoomDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString * str ;
    if ([self.model.partition_name isEqualToString:@""]) {
        str = @"";
    }else{
        str = [NSString stringWithFormat:@"%@，",self.model.partition_name];
    }
    NSString * str1;
    if ([self.model.building_name isEqualToString:@""]) {
        str1 = @"";
    }else{
        str1 = [NSString stringWithFormat:@"%@",self.model.building_name];
    }
    NSString * str2;
    if ([self.model.building_unit isEqualToString:@""]) {
        str2 = @"";
    }else{
        str2 = [NSString stringWithFormat:@"%@，",self.model.building_unit];
    }
    NSString * str3;
    if ([self.model.unit_name isEqualToString:@""]) {
        str3 = @"";
    }else{
        str3 = [NSString stringWithFormat:@"%@",self.model.unit_name];
    }
    NSString * str4 ;
    if ([self.model.unit_unit isEqualToString:@""]) {
        str4 = @"";
    }else{
        str4 = [NSString stringWithFormat:@"%@，",self.model.unit_unit];
    }
    
    self.houseNumTitle.text = [NSString stringWithFormat:@"%@%@%@%@%@%@(室)",str,str1,str2,str3,str4,self.model.house_name];
//    self.houseNumInfo.text = [NSString stringWithFormat:@"%@ | 建面:%@㎡ | 套内:%@㎡",self.model.layout,self.model.house_area,self.model.actual_area];
    if ([self.model.actual_area integerValue] == 0) {
        self.houseNumInfo.text = [NSString stringWithFormat:@"%@ | 建面:%@㎡",self.model.layout,self.model.house_area];
    }else{
        self.houseNumInfo.text = [NSString stringWithFormat:@"%@ | 建面:%@㎡ | 套内:%@㎡",self.model.layout,self.model.house_area,self.model.actual_area];
    }
    self.houseNumFollw.text = self.model.love_count;
    [self.houseNumPhoto sd_setImageWithURL:[NSURL URLWithString:_model.pic_url?:@""] placeholderImage:[UIImage imageNamed:kDefaultImageName]];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;

    [self.direction sd_setImageWithURL:[NSURL URLWithString:[self.deModel valueForKey:self.model.orientation_pic]?:@""] placeholderImage:[UIImage imageNamed:kDefaultImageName]];
    
    //时间计算
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    
    
    dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSDate *localeDate =[dateFormat dateFromString:self.proModel.net_sale_start];
    
    //系统当前时间
    NSDate *nowDate = [Utils getInternetDate];
    
    dateResult = [Utils compareOneDay:nowDate withAnotherDay:localeDate];
    
    if ([self.proModel.net_sale integerValue] == 0 || [self.userModel.shop_code_used integerValue] ==1 || [self.userModel.has_buy_house integerValue] ==1 || [self.userModel.deposit_return integerValue] ==1 || [self.model.status integerValue] == 0 || [self.model.status integerValue] > 1) {
        self.addBtn.hidden = YES;
    }
    [self.addBtn setImage:[UIImage imageWithContentsOfFile:[Utils imgPath:@"zxgf_btn@3x.png"]] forState:0];
    if([self.proModel.net_sale integerValue] == 1 && (dateResult == 0 || dateResult == 1)){
        
        [self.addBtn setTitle:@"立即购买" forState:0];
    }
#warning 项目改动
    if ([allConstProjectid integerValue] != 7) {
        [self.addBtn setTitleColor:[Utils colorWithHexString:@"ffc000"] forState:0];
    }
}

//加入抢购
- (IBAction)addBtn:(UIButton *)sender {
    
    
    if ([self.proModel.net_sale integerValue] == 0) {
        
        [HUD showMsg:@"暂未开通网上售房服务"];
        
    }else if([self.proModel.net_sale integerValue] == 1){
        
        if ([self.userModel.authenticated integerValue] !=1) {
            [HUD showMsg:@"请先完成实名认证"];
            return;
        }
        if ([self.userModel.shop_code_used integerValue] ==1) {
            [HUD showMsg:@"购房码已使用"];
            return;
        }
        if ([self.userModel.is_sincere integerValue] !=1) {
            [HUD showMsg:@"请完成提前报名"];
            return;
        }
        if ([self.userModel.has_buy_house integerValue] ==1) {
            [HUD showMsg:@"只能抢购一套房源"];
            return;
        }
        if ([self.userModel.deposit_return integerValue] ==1) {
            [HUD showMsg:@"请完成提前报名(诚意金已退)"];
            return;
        }
//        if (dateResult == -1) {
//            [HUD showMsg:@"未到购房时间"];
//            return;
//        }
        if ([self.model.status integerValue] == 0) {
            [HUD showMsg:@"房源未开售"];
            return;
        }
        if ([self.model.status integerValue] > 1) {
            [HUD showMsg:@"房源已被抢"];
            return;
        }
        
        if ([self.addBtn.titleLabel.text isEqualToString:@"立即购买"]) {
            self.alertController = [UIAlertController alertControllerWithTitle:@"注意" message:@"您只能抢购一套房子，是否确认抢购当前户型？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.alertController dismissViewControllerAnimated:YES completion:nil];
            }];
            
            [self.alertController addAction:resetAction];
            UIAlertAction *resetAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                if (_delegate != nil) {
                    
                    [_delegate buyHouse:self.model];
                    [self dismissSemiModalView];
                }
                
            }];
            
            [self.alertController addAction:resetAction1];
            [self presentViewController:_alertController animated:YES completion:nil];
            
        }else{
    
    NSDictionary *parameter = [LRNetwork requestParameter:@{@"house_id":self.model.house_id}];
    NSString *url = [LRNetwork requestURL:@"cgi/house_select_add.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        [HUD showSucceedMsg:@"添加成功!"];
        
        [self dismissSemiModalView];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
    }
    }
}

- (IBAction)closeBtn:(UIButton *)sender {
    
     [self dismissSemiModalView];
}
- (IBAction)chaoxiangTap:(UITapGestureRecognizer *)sender {
    tapStr = @"朝向";
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    // Set options
    browser.displayActionButton = NO;
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    [browser setCurrentPhotoIndex:1];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    
    nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:nc animated:YES completion:nil];
}

- (IBAction)tagClick:(UITapGestureRecognizer *)sender {
    tapStr = @"户型";
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    // Set options
    browser.displayActionButton = NO;
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    [browser setCurrentPhotoIndex:1];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    
    nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:nc animated:YES completion:nil];
}
#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
{
    return 1;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
{
    if ([tapStr isEqualToString:@"户型"]) {
        
        return [MWPhoto photoWithImage:self.houseNumPhoto.image];
        
    }else return [MWPhoto photoWithImage:self.direction.image];
}

- (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)hide{
    
    [self.alertController dismissViewControllerAnimated:YES completion:nil];
    
}
@end
